<template>
  <div class="dashboard-editor-container">
    <el-row :gutter="40">
      <el-col :sm="24" :xs="24" :md="6" :xl="6" :lg="6" :style="{ marginBottom: '12px' }">
        <div @click="queryAllList">
          <chart-card title="申请总数" :total="totalInfo.applyTotal" style="cursor: pointer" background="rgb(77, 112, 255)">
            <img slot="icon" src="@/assets/all.png" alt="">
          </chart-card>
        </div>
      </el-col>
      <el-col :sm="24" :xs="24" :md="6" :xl="6" :lg="6" :style="{ marginBottom: '12px' }">
        <div @click="queryCompleteList">
          <chart-card title="已完成" :total="totalInfo.finishTotal" style="cursor: pointer" background="rgb(103, 194, 58)">
            <img slot="icon" src="@/assets/complete.png" alt="">
          </chart-card>
        </div>
      </el-col>
      <el-col :sm="24" :xs="24" :md="6" :xl="6" :lg="6" :style="{ marginBottom: '12px' }">
        <div @click="queryProcessList">
          <chart-card title="进行中" :total="totalInfo.runTotal" style="cursor: pointer" background="rgb(230, 162, 60)">
            <img slot="icon" src="@/assets/process.png" alt="">
          </chart-card>
        </div>
      </el-col>
      <el-col :sm="24" :xs="24" :md="6" :xl="6" :lg="6" :style="{ marginBottom: '12px' }">
        <div @click="queryProcessList">
          <chart-card title="年审中" :total="totalInfo.yearTotal" style="cursor: pointer" background="rgb(230, 162, 60)">
            <img slot="icon" src="@/assets/process.png" alt="">
          </chart-card>
        </div>
      </el-col>
    </el-row>

    <el-card class="box-card">
      <el-form ref="listQuery" :model="listQuery" :inline="true">
        <el-form-item label="项目名称" prop="title">
          <el-input v-model="listQuery.name" placeholder="请输入项目名称" clearable size="small" style="width: 180px" @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item label="申报人/填报人">
          <el-input v-model="listQuery.applyUser" clearable size="small" style="width: 180px" @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item label="开通时间">
          <el-date-picker
            v-model="timeValue"
            style="width: 300px;"
            clearable
            size="small"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            align="right"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        border
        :data="ticketList"
        :header-cell-style="{ textAlign: 'center' }"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="#" type="index" width="80" align="center" />
        <el-table-column label="项目名称" prop="name" :show-overflow-tooltip="true" align="center" />
        <el-table-column label="上报时间" align="center" prop="fillTime" width="180" />
        <el-table-column label="负责人姓名" :show-overflow-tooltip="true" prop="applyManagerName" align="center" width="120" />
        <el-table-column label="上报人" prop="applyUserName" :show-overflow-tooltip="true" align="center" />
        <el-table-column label="收费价格开通日期" prop="openTime" align="center" />
        <el-table-column label="最后一次年审日期" prop="lastYearAuditTime" align="center" />
        <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button v-permisaction="['history:XjsAnnualQuery']" size="mini" type="text" icon="el-icon-s-check" @click="handleNsView(scope.row)">年审记录</el-button>
            <el-button v-permisaction="['queryProject:XjsAnnualQuery']" size="mini" type="text" icon="el-icon-s-check" @click="handleView(scope.row)">项目详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageInfo.total > 0" :total="pageInfo.total" :page.sync="pageInfo.currentPage" :limit.sync="pageInfo.pageSize" @pagination="getList" />
    </el-card>
    <HistoryDialog ref="historyDialog" />
    <ReviewDialog ref="reviewDialog" @refreshTable="getList" />
    <RecordDialog v-if="recordVisible" ref="recordRef" :visible.sync="recordVisible" />
  </div>
</template>

<script>
import {
  doYearAuditApplication,
  doYearAuditTotal
} from '@/api/xjs/work-order'

import HistoryDialog from '@/views/xjsxxm/components/history'
import ReviewDialog from '../components/review'
// 搜索
import ChartCard from './components/ChartCard'
import RecordDialog from '@/views/xjsxxm/components/annualRecords.vue'

export default {
  name: 'DashboardAdmin',
  components: {
    ChartCard,
    HistoryDialog,
    ReviewDialog,
    RecordDialog
  },
  data() {
    return {
      users: [],
      userMap: {},
      nodeList: [],
      nsVisible: false,
      dialogVisible: false,
      recordVisible: false,
      queryParams: {},
      total: 0,
      loading: false,
      ticketList: [],
      currentNs: [],
      timeValue: [],
      listQuery: {
        name: '', // 项目名称
        applyUser: '', // 申报人
        startOpeningTime: '', // 开通开始时间
        endOpeningTime: '' // 开通结束时间
      },
      listQueryCopy: null,
      pageInfo: { // 分页信息
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      totalInfo: {
        applyTotal: 0,
        finishTotal: 0,
        runTotal: 0,
        yearTotal: 0
      },
      downloadLoading: false,
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  watch: {
    // 处理申报时间
    timeValue: {
      handler(newVal, oldVal) {
        if (newVal && newVal.length) {
          this.listQuery.startOpeningTime = newVal[0]
          this.listQuery.endOpeningTime = newVal[1]
        } else {
          this.listQuery.startOpeningTime = ''
          this.listQuery.endOpeningTime = ''
        }
      },
      deep: true
    }
  },
  created() {
    this.listQueryCopy = JSON.parse(JSON.stringify(this.listQuery))
    this.getList()
    this.getInitData()
  },
  methods: {
    handleNsView(row) {
      this.recordVisible = true
      this.$nextTick(() => {
        this.$refs.recordRef.open(row.otherId)
      })
      // this.$alert('暂无年审记录', '提示', {
      //   confirmButtonText: '确定'
      // })
    },
    handleExportList() {},
    handleExport(row) {},
    // 重置
    resetQuery() {
      this.timeValue = []
      this.listQuery = JSON.parse(JSON.stringify(this.listQueryCopy))
      this.getList()
    },
    // 获取数据
    getInitData() {
      doYearAuditTotal(this.queryList).then(response => {
        if (response) {
          const data = response.records[0]
          this.totalInfo = data
        }
      })
    },
    queryAllList() {},
    queryCompleteList() {},
    queryProcessList() {},
    getList() {
      this.loading = true
      const params = {
        ...this.listQuery,
        ...this.pageInfo
      }
      doYearAuditApplication(params).then(res => {
        this.ticketList = res.records
        this.pageInfo.total = res.total
        this.loading = false
      })
    },
    // 表单数据弹窗
    handleView(row) {
      // this.$refs.reviewDialog.open(row.process, row.id)
      // this.$refs.reviewDialog.getNewDeptTreeList()
      this.$refs.reviewDialog.open(row)
    },
    handleHistoryView(row) {
      this.$refs.historyDialog.open(row.process, row.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
    padding: 12px;
    background-color: rgb(240, 242, 245);
    position: relative;

    .github-corner {
        position: absolute;
        top: 0;
        border: 0;
        right: 0;
    }

    .chart-wrapper {
        background: #fff;
        padding: 16px 16px 0;
        margin-bottom: 32px;
    }
}

::v-deep .el-tabs__item {
    padding-left: 16px !important;
    height: 50px;
    line-height: 50px;
}

@media (max-width: 1024px) {
    .chart-wrapper {
        padding: 8px;
    }
}

.leaderboard {
    height: 448px;
    overflow: auto;
}
</style>
